package cn.cpf.beantest;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;


public class Person implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, ResourceLoaderAware, InitializingBean, DisposableBean {
    private String name;
    private String address;
    private int phone;

    // BeanNameAware, BeanFactoryAware, ApplicationContextAware, ResourceLoaderAware, ServletContextAware
    private String beanName;
    private BeanFactory beanFactory;
    private ApplicationContext applicationContext;
    private ResourceLoader resourceLoader;

    public Person() {
        System.out.println("【构造器】调用Person的构造器实例化");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("【注入属性】name");
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        System.out.println("【注入属性】address");
        this.address = address;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        System.out.println("【注入属性】phone");
        this.phone = phone;
    }

    public void setBeanName(String s) {
        System.out.println("【BeanNameAware接口】调用 setBeanName 方法");
        this.beanName = s;
    }

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("【BeanFactoryAware接口】调用 setBeanFactory 方法");
        this.beanFactory = beanFactory;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("【ApplicationContextAware】调用 setApplicationContext 方法");
        this.applicationContext = applicationContext;
    }

    public void setResourceLoader(ResourceLoader resourceLoader) {
        System.out.println("【ResourceLoaderAware】调用 setResourceLoader 方法");
        this.resourceLoader = resourceLoader;
    }


    // 这是 InitializingBean 接口方法
    public void afterPropertiesSet() throws Exception {
        System.out.println("【InitializingBean接口】调用afterPropertiesSet方法");
    }



    // 通过<bean>的init-method属性指定的初始化方法
    public void myInit() {
        System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
    }

    // 通过<bean>的destroy-method属性指定的初始化方法
    public void myDestory() {
        System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
    }


    @Override
    public String toString() {
        return "Person{" + "name='" + name + '\'' + ", address='" + address + '\'' + ", phone=" + phone + ", beanName='" + beanName + '\'' + ", beanFactory=" + beanFactory + ", applicationContext=" + applicationContext + ", resourceLoader=" + resourceLoader + '}';
    }

    public void destroy() throws Exception {

    }
}
